package com.black.utils.network.app.sftp;

import com.black.utils.commons.pool.PoolFactory;
import com.black.utils.commons.pool.PoolProperties;
import com.jcraft.jsch.JSchException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ylx
 */
@Slf4j
@Getter
@Setter
public class SFTPClientFactory extends PoolFactory<SFTPClient> {
    public SFTPClientFactory(PoolProperties poolProperties) {
        super(poolProperties);
    }

    @Override
    public SFTPClient make() throws JSchException {
        return new SFTPClient(poolProperties);
    }

    @Override
    public void destroy(SFTPClient sftpClient) {
        if (sftpClient.isConnected()) {
            sftpClient.close();
        }
    }

    @Override
    public boolean validate(SFTPClient sftpClient) {
        return sftpClient.isConnected();
    }

    @Override
    public void active(SFTPClient sftpClient) throws Exception {
        if (!sftpClient.isConnected()) {
            sftpClient.connect();
        }
    }

    @Override
    public void passivate(SFTPClient sftpClient) {
    }
}
